package com.ucode.cgms.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ucode.cgms.mapper.CgmsAfterSaleCompanionOrderMapper;
import com.ucode.model.cgms.CgmsAfterSaleCompanionOrder;
import com.ucode.model.cgms.enums.CgmsSysResultCodeEnum;
import com.ucode.springboot.starter.mybatis.core.base.BaseService;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 陪玩订单售后订单服务
 * @Author: liliang
 * @Date: 2025/5/23 16:59
 * @Version: 1.0
 */
public interface CgmsAfterSaleCompanionOrderService extends BaseService<Long,CgmsAfterSaleCompanionOrder, CgmsAfterSaleCompanionOrderMapper> {

    /**
     * 提交申请
     * @param bean
     */
    public void apply(CgmsAfterSaleCompanionOrder bean);

    /**
     * 取消售后[管理员取消]
     * @param id
     */
    public void cancel(Long id);

    /**
     * 取消售后【用户取消，带校验】
     * @param userId
     * @param id
     */
    public void cancelByMember(Long userId,Long id);
    /**
     * 删除售后
     * @param userId
     * @param id
     */
    public void deleteByMember(Long userId,Long id);
    /**
     * 售后审核
     * @param id
     * @param statuz 参考 CgmsAfterSaleCompanionOrderStatusEnum 取值范围【2、3】
     * @param handlerId 处理人
     * @param handlerType 处理人类型
     * @param handleNote 处理说明
     */
    public void audit(Long id,Integer statuz,Long handlerId,Integer handlerType,String handleNote);

    /**
     * 退款成功，渠道退款成功
     * @param id
     * @param refundStatementId
     * @param refundTime
     * @param refundAmount
     */
    void refundSuccessAfterSale(Long id, Long refundStatementId, LocalDateTime refundTime, Integer refundAmount);

    /**
     * 校验
     * @param id
     * @return
     */
    public default CgmsAfterSaleCompanionOrder validate(Long id){
        if(ObjectUtil.isNull(id) )
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);
        //验证数据是否存在
        CgmsAfterSaleCompanionOrder bean = this.selectByPrimaryKey(id);
        if(ObjectUtil.isNull(bean) || bean.getDeltag()){
            throw new UcodeServiceException(CgmsSysResultCodeEnum.AFTERSALE_COMPANION_ORDER_NOT_EXISTS);
        }
        return bean;
    }

    /**
     * 校验
     * @param id
     * @return
     */
    public default CgmsAfterSaleCompanionOrder validate(Long id,Long userId){
        CgmsAfterSaleCompanionOrder bean = this.validate(id);

        if(!ObjUtil.equals(bean.getUserId(),userId)){
            throw new UcodeServiceException(GlobalResultCodeEnum.UN_DATA_PERMISSION);
        }
        return bean;
    }

    /**
     * 根据ID列表获取对象Map
     * @param ids ID集合
     * @return Map<ID, 对象>
     */
    default Map<Long, CgmsAfterSaleCompanionOrder> getMap(Collection<Long> ids) {
        return CollUtil.isEmpty(ids) ? new HashMap<>() : CollectionUtils.convertMap(this.selectByPrimaryKeys(ids), CgmsAfterSaleCompanionOrder::getId);
    }
}
